package com.watson.quartzdemo.job;

import com.watson.quartzdemo.config.SpringContextJobUtil;
import com.watson.quartzdemo.service.MyService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实现Job接口,重写execute方法在里面操作我们要执行的业务逻辑
 * @author AdminWatson
 */
@Slf4j
public class MyJob implements Job {
    @Resource
    private MyService myService;
    @Resource
    private Scheduler scheduler;

    public MyJob() {
        this.myService = (MyService) SpringContextJobUtil.getBean("myService");
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            JobKey key = jobExecutionContext.getJobDetail().getKey();
            log.info("任务开始执行了"+key.getGroup()+key.getName());
            JobDataMap jobDataMap = jobExecutionContext.getTrigger().getJobDataMap();
            System.out.println("jobDataMap"+jobDataMap.get(key));
            JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
            System.out.println("mergedJobDataMap"+mergedJobDataMap);

            executeTask();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        log.info("任务执行结束了");

    }

    private void executeTask() throws SchedulerException {
        myService.bizFunction();
    }

}